package org.ioc.com.mybatis_demo3.mapper;

import org.apache.ibatis.annotations.*;
import org.ioc.com.mybatis_demo3.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    //使用注解操作数据库的添加
    @Insert("insert into userinfo(username,password,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer insert(UserInfo userInfo);
    //使用注解操作数据库的删除
    @Delete("delete from userinfo where id=#{id}")
    Integer delete(Integer id);
    //使用注解操作数据库的修改
    @Update("update userinfo set gender=#{gender} where id=#{id}")
    Integer update(Integer gender,Integer id);
    //使用注解操作数据库的查询
    /*
    由于数据库的字段和Java对象的属性名字没有对应上,所以输出的结果有的为空,对此我们有以下三种方式解决
    1.改别名
    2.映射
    3.驼峰转换
     */
    @Select("select * from userinfo")
    List<UserInfo> getUserAll();
    //1.改别名
    @Select("select id,username,age,gender,phone,delete_flag as deleteFlag," +
            "create_time as createTime,update_time as updateTime from userinfo")
    List<UserInfo> getUserAll1();
    //2.映射
    @Results(id= "resultMap",value = {
            @Result(column = "delete_flag",property = "deleteFLag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime"),
    })
    @Select("select * from userinfo")
    List<UserInfo> getUserAll2();


    @ResultMap("resultMap")
    @Select("select * from userinfo")
    List<UserInfo> getUserAll3();
    //3.驼峰转换
    @Select("select * from userinfo")
    List<UserInfo> getUserAll4();
}
